package com.example.a14874.listviewtraining;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    private ListView stuListView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        stuListView = findViewById(R.id.name_ListView);

        initStuList();
        adapter = new StuAdapter(MainActivity.this, studentArrayList);
        stuListView.setAdapter(adapter);

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student s =  studentArrayList.get(i);
                Toast.makeText(MainActivity.this,
                        "姓名"+s.getName() + " 年龄"
                                 +  s.getAge(),
                        Toast.LENGTH_SHORT)
                        .show();

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                intent.putExtra("NAME", s.getName());
                intent.putExtra("AGE", s.getAge());
                intent.putExtra("IMGID",s.getImgid());
                startActivity(intent);
            }
        });


    btnAddStu = findViewById(R.id.btn_StartAddStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,
                    AddActivity.class);
            startActivityForResult(intent, 1001);
        }
    });
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("MainActivity",
                "requestCode = " + requestCode + " resultCode = " + resultCode);

        if(data == null){
            return;
        }
        int imgId = data.getIntExtra("IMG", R.drawable.dog1);
        String name = data.getStringExtra("NAME");
        int age = data.getIntExtra("AGE", 0);


        Student s = new Student(imgId,name, age);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();

    }

    private void initStuList(){
        Student s1 = new Student(R.drawable.dog1,"张三",18);
        Student s2 = new Student(R.drawable.dog1,"张是",19);
        Student s3 = new Student(R.drawable.dog1,"张发",10);
        Student s4 = new Student(R.drawable.dog1,"张吧",11);
        Student s5 = new Student(R.drawable.dog1,"张把",13);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }
}
